package com.example.finalcountdown;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private Button startButton;
	private Button resetButton;
	private Button addLap;
	private Button removeLap;
	private Button addSecondButton;
	private Button removeSecondButton;
	private TextView count;
	private TextView laps;
	private TextView lapTime;
	private int lapsCount = 8;
	private CountDownTimer timer;
	private int roundTime = 20;
	private int breakTime = 10;
	private boolean isTiming = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//setting up UI
		startButton = (Button) findViewById(R.id.start);
		resetButton = (Button) findViewById(R.id.reset);
		addSecondButton = (Button) findViewById(R.id.addSecond);
		removeSecondButton = (Button) findViewById(R.id.removeSecond);
		addLap = (Button) findViewById(R.id.addLap);
		removeLap = (Button) findViewById(R.id.removeLap);
		count = (TextView) findViewById(R.id.count);
		laps = (TextView) findViewById(R.id.laps);
		lapTime = (TextView) findViewById(R.id.lapTime);
		
		startButton.setOnClickListener(this);
		resetButton.setOnClickListener(this);
		addSecondButton.setOnClickListener(this);
		removeSecondButton.setOnClickListener(this);
		addLap.setOnClickListener(this);
		removeLap.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
//	public void sendMessage(View view) {
//		Intent intent = new Intent(this, DisplayNewActivity.class);
//		EditText editText = (EditText) findViewById(R.id.edit_msg);
//		String msg = editText.getText().toString();
//		intent.putExtra(EXTRA_MSG, msg);
//		startActivity(intent);
//	}
	
	public void startTimer(){
		while(lapsCount > 0) {
			timer = new CountDownTimer(roundTime * 1000, 1000) {
	

				@Override
				public void onFinish() {
					new CountDownTimer(breakTime * 1000, 1000) {

						@Override
						public void onFinish() {
							lapsCount--;
						}

						@Override
						public void onTick(long millisUntilFinished) {
							count.setText("" + millisUntilFinished / 1000);
							
						}
						
					}.start();
					
				}

				@Override
				public void onTick(long millisUntilFinished) {
					count.setText("" + millisUntilFinished / 1000);
					
				}
				
			}.start();
		}
	}
	public void reset() {
		String reset = "reset";
		Activity activity;
	}

	@Override
	public void onClick(View v) {
		if(v == startButton) {
			startTimer();
		}
		else if(v == resetButton) {
			reset();
		}
		else if(v == addLap) {
			lapsCount++;
		}
		else if(v == removeLap) {
			lapsCount--;
		}
		else if(v == addSecondButton) {
			roundTime++;
		}
		else if(v == removeSecondButton) {
			roundTime--;
		}
	}

}
